home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 351-375 / disk_360 / uucp / uucp0.lzh / src / anews / mscan.c < prev    next >
C/C++ Source or Header  |  1990-04-10  |  5KB  |  269 lines

  1.  
  2. /*
  3.  *  MSCAN.C
  4.  *
  5.  * mscan - Scan directories and return a sorted list of file names,
  6.  *        one at a time.
  7.  *
  8.  *  This code originally taken from sysdep.c:
  9.  *
  10.  * (C) Copyright 1987 by John Gilmore
  11.  * Copying and use of this program are controlled by the terms of the Free
  12.  * Software Foundation's GNU Emacs General Public License.
  13.  *
  14.  * Amiga Changes Copyright 1988 by William Loftus.  All rights reserved.
  15.  *
  16.  * Rewrite for nlib Copyright 1990 by J. Gregory Noel.    All rights reserved.
  17.  */
  18.  
  19. #include "news.h"
  20. #include <ctype.h>
  21. #include <getfiles.h>
  22. #include <expand_path.h>
  23.  
  24. #define GAP    2
  25.  
  26. extern char *NewsDir;        /* Path to news directory */
  27. static dir_list *files;     /* List of articles to be examined */
  28. static dir_list *curptr;    /* Pointer to current article */
  29. static char *curgroup;        /* Name of current group */
  30.  
  31. static int    /* select only files that are purely numeric */
  32. filesel(char *a)
  33. {
  34.     while (*a != '\0') {
  35.         if (!isdigit(*a))
  36.             return 0;
  37.         ++a;
  38.     }
  39.     return 1;
  40. }
  41.  
  42. static int    /* sort based upon the numeric value of the filename */
  43. filecmp(dir_list *a, dir_list *b)
  44. {
  45.     register long i;
  46.  
  47.     i = atol(a->name + GAP) - atol(b->name + GAP);
  48.     if (i != 0) return i > 0;
  49.     return strcmp(a->name, b->name);
  50. }
  51.  
  52. void        /* remove files marked for deletion */
  53. free_directory(delart)
  54. int delart;
  55. {
  56.     register dir_list *p;
  57.     register int last;
  58.  
  59.     last = -1;
  60.     while (p = files) {
  61.     files = p->next;
  62.     if (p->name[0]) {
  63.         if (delart)
  64.         remove(expand_path(curgroup, p->name + GAP));
  65.     } else if (p->name[1])
  66.         last = atoi(p->name + GAP);
  67.     else if (last < 0)
  68.         last = 0;
  69.     free(p);
  70.     }
  71.     if (last >= 0) {
  72.     register FILE *fp;
  73.     if (fp = fopen(expand_path(curgroup, ".read"), "w")) {
  74.         fprintf(fp, "%d", last);
  75.         fclose(fp);
  76.     }
  77.     } else if (delart) {
  78.     /* all files were deleted -- try to delete directory */
  79.     remove(expand_path(curgroup, ".next"));
  80.     remove(expand_path(curgroup, ".read"));
  81.     rmdir(curgroup);
  82.     }
  83.     curptr = NULL;
  84.     if (curgroup) {
  85.     free(curgroup);
  86.     curgroup = NULL;
  87.     }
  88. }
  89.  
  90. int        /* produce a list of articles in the group */
  91. scan_directory(char *dir)
  92. {
  93.     register dir_list *p;
  94.     register int count = 0;
  95.     register FILE *fp;
  96.     int read;
  97.     char name[100], newname[100];
  98.     extern int unpackmail(char *dir, int count);
  99.  
  100. recurse:
  101.     free_directory(0);
  102.     curgroup = strdup((dir == NULL) ? NewsDir : expand_path(NewsDir, dir));
  103.     if (access(curgroup, 0) == -1) {
  104.         free(curgroup), curgroup = NULL;
  105.         return 0;
  106.     }
  107.  
  108.     if ((files = getfiles(curgroup, GAP, filesel, filecmp)) == NULL) {
  109.         if (unpackmail(curgroup, 0) > 0) {
  110.             /* return scan_directory(dir); */
  111.             goto recurse;
  112.         }
  113. #ifdef NOTDEF    /* DON'T DELETE THE !#@$#@ DIRECTORY IDIOT  */
  114.         /* no files in directory -- try to delete directory */
  115.         remove(expand_path(curgroup, ".next"));
  116.         remove(expand_path(curgroup, ".read"));
  117.         rmdir(curgroup);
  118. #endif
  119.         return 0;
  120.     }
  121.  
  122.     read = 0;
  123.     if ((fp = fopen(expand_path(curgroup, ".read"), "r")) != NULL) {
  124.         fscanf(fp, "%d", &read);
  125.         fclose(fp);
  126.     }
  127.  
  128.     for (p = files; p != NULL; p = p->next) {
  129.         p->name[0] = 0;
  130.         p->name[1] = (atoi(p->name + GAP) <= read);
  131.         sprintf(name, "%s/%s", curgroup, p->name + GAP);
  132.         sprintf(newname, "%s/%d", curgroup, ++count);
  133.         if (strcmp(name, newname) != 0)
  134.             if (rename(name, newname) == 0)
  135.                 sprintf(p->name + GAP, "%d", count);
  136.     }
  137.  
  138.     if (unpackmail(curgroup, count) > count)
  139.         return scan_directory(dir);
  140.  
  141.     if ((fp = fopen(expand_path(curgroup, ".next"), "w")) != NULL) {
  142.         fprintf(fp, "%d", count + 1);
  143.         fclose(fp);
  144.     }
  145.     return count;
  146. }
  147.  
  148. char *
  149. first_unread(void)
  150. {
  151.     register dir_list *p;
  152.  
  153.     for (p = files; p != NULL; p = p->next) {
  154.         if (p->name[1] == 0) {
  155.             curptr = p;
  156.             return p->name + GAP;
  157.         }
  158.     }
  159.     curptr = NULL;
  160.     return NULL;
  161. }
  162.  
  163. int
  164. unread_count(void)
  165. {
  166.     register dir_list *p;
  167.     register int count = 0;
  168.  
  169.     for (p = files; p != NULL; p = p->next)
  170.         if (p != curptr && p->name[1] == 0)
  171.             ++count;
  172.     return count;
  173. }
  174.  
  175. char *
  176. goto_article(char *name)
  177. {
  178.     register dir_list *p;
  179.  
  180.     for (p = files; p != NULL; p = p->next) {
  181.         if (strcmp(p->name + GAP, name) == 0) {
  182.             curptr = p;
  183.             return p->name + GAP;
  184.         }
  185.     }
  186.     curptr = NULL;
  187.     return NULL;
  188. }
  189.  
  190. char *
  191. get_next_art(void)
  192. {
  193.     if (curptr == NULL)
  194.         curptr = files;
  195.     else
  196.         curptr = curptr->next;
  197.     return (curptr == NULL) ? NULL : curptr->name + GAP;
  198. }
  199.  
  200. char *
  201. get_prev_art(void)
  202. {
  203.     register dir_list *p;
  204.  
  205.     if (curptr == NULL)
  206.         return NULL;
  207.     if (curptr == files)
  208.         return curptr->name + GAP;
  209.     for (p = files; p != NULL; p = p->next) {
  210.         if (p->next == curptr) {
  211.             curptr = p;
  212.             return p->name + GAP;
  213.         }
  214.     }
  215.     /* should never get here... */
  216.     curptr = NULL;
  217.     return NULL;
  218. }
  219.  
  220. void
  221. rewind_arts(void)
  222. {
  223.     register dir_list *p;
  224.  
  225.     for (p = files; p != NULL; p = p->next)
  226.         if (p->name[0] == 0)
  227.             p->name[1] = 0;
  228.     curptr = NULL;
  229. }
  230.  
  231. void
  232. mark_cur_art(int flag)
  233. {
  234.     if (curptr != NULL)
  235.         curptr->name[1] = flag;
  236. }
  237.  
  238. void
  239. del_cur_art(int flag)
  240. {
  241.     if (curptr != NULL)
  242.         curptr->name[0] = flag;
  243. }
  244.  
  245. void
  246. hold_cur_art(void)
  247. {
  248.     if (curptr != NULL)
  249.         curptr->name[1] = curptr->name[0];
  250. }
  251.  
  252. #ifdef TEST
  253. #undef TEST
  254. #include "unpackmail.c"
  255. char *NewsDir;
  256.  
  257. main(int argc, char **argv)
  258. {
  259.     register char *p;
  260.  
  261.     NewsDir = GetConfigDir(UUNEWS);
  262.     printf("%d articles:\n", scan_directory("mail.test"));
  263.     while ((p = get_next_art()) != NULL)
  264.         printf("%s ", p);
  265.     putchar('\n');
  266.     return 0;
  267. }
  268. #endif
  269.